package com.np.retrofit211demo1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.np.retrofit211demo1.bean.AccessTokenBean;
import com.np.retrofit211demo1.bean.SystemResponse;
import com.np.retrofit211demo1.retrofit.HttpConfig;
import com.np.retrofit211demo1.retrofit.LiveDataCenter;
import com.np.retrofit211demo1.retrofit.RetrofitFactory;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    public MainActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getAccessToken();
    }

    private void getAccessToken() {
        RetrofitFactory.getInstance().getSystemService().login(RetrofitFactory.getJSONRequestBodyFromParams(
                        "username", "admin",
                        "password", "admin@123456",
                        "mobile", "15888888888",
                        "captcha", "10086"))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new io.reactivex.Observer<SystemResponse<AccessTokenBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        // 请求中
                    }

                    @Override
                    public void onNext(SystemResponse<AccessTokenBean> o) {
                        if (o.getCode() == HttpConfig.CODE_SUCCESS) {
                            LiveDataCenter.setAccessToken(o.getData());
                            Log.i(TAG, "getAccessToken. data: " + o.getData());
                        } else {
                            Log.i(TAG, "getAccessToken. message: " + o.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i(TAG, "登录失败. " + e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}